package com.ag.processing;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ag.bean.TbCurrentEvent;
import com.ag.bean.TbLogPlanStep;
import com.ag.bean.TbLogPsContent;
import com.ag.common.AgException;
import com.ag.zrt.dao.TbCurrentEventDAO;

public class ProcessingPlans extends HibernateDaoSupport {

	// 已启动预案列表
	public static List<List<TbLogPlanStep>> processingPlans = new ArrayList<List<TbLogPlanStep>>(
			0);

	// 各预案列表正处于的环节下标
	public static List<Integer> processingSteps = new ArrayList<Integer>(0);
	@Resource(name="ZRTTbCurrentEventDAO")
	private TbCurrentEventDAO curEventDAO;
	
	/*
	 * 删除当前处理事件表中办结的事件
	 */
	public void finnishEvent(String eventId) {
		if (eventId != null) {
			try {
				TbCurrentEvent ce = new TbCurrentEvent();
				ce.setEId(eventId);

				curEventDAO.delete(ce);
			} catch (AgException e) {
				e.printStackTrace();
				throw e;
			}
		}
	}

	/*
	 * 获取当前处理事件的当前步骤ID
	 */
	public String getCurrentStepId(String eventId) {
		try {
			TbCurrentEvent ce = (TbCurrentEvent) curEventDAO.findById(eventId);
			return ce.getCurStepId();
		} catch (AgException e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 根据下标获取已启动预案的预案步骤
	 * 
	 * @param index
	 * @return
	 */
	public ProcessingPlan getProcessingPlan(int index) {
		checkException();
		if (processingPlans.size() != 0)
			return getNewProcessingPlan(processingPlans.get(index),
					processingSteps.get(index), index);
		return null;
	}

	/**
	 * 设置启动预案
	 */
	public int setLogPlan() {
		checkException();
		return 0;
	}

	/**
	 * 根据预案获取预案的默认内容
	 * 
	 * @param planStep
	 * @return
	 */
	public Set<TbLogPsContent> getLogPsContent(TbLogPlanStep logPlanStep) {
		return null;
	}

	/**
	 * 根据上级LogPlanStep来对LogPlanSteps列表进行插入操作
	 */
	public void addLogPlanStep(TbLogPlanStep logPlanStep) {
		checkException();
	}

	/**
	 * 进入下一环节
	 */
	public void enterNextStep(int index) {
		Integer i = processingSteps.get(index);
		i += 1;
		processingSteps.set(index, i);
	}

	private void checkException() {
		if (processingPlans.size() != processingSteps.size()) {
			throw new RuntimeException("ProcessingPlans——已启动预案数目出错！");
		}
	}

	private ProcessingPlan getNewProcessingPlan() {
		return new ProcessingPlan();
	}

	private ProcessingPlan getNewProcessingPlan(
			List<TbLogPlanStep> logPlanSteps, int currentStep, int listStamp) {
		return new ProcessingPlan(logPlanSteps, currentStep, listStamp);
	}

	public class ProcessingPlan {
		private List<TbLogPlanStep> logPlanSteps;
		private int currentStep;
		private int listIndex;// 当前启动预案列表下标

		public ProcessingPlan() {
			super();
		}

		public ProcessingPlan(List<TbLogPlanStep> logPlanSteps,
				int currentStep, int listStamp) {
			super();
			this.logPlanSteps = logPlanSteps;
			this.currentStep = currentStep;
			this.listIndex = listStamp;
		}

		private List<TbLogPlanStep> getLogPlanSteps() {
			return logPlanSteps;
		}

		private void setLogPlanSteps(List<TbLogPlanStep> logPlanSteps) {
			this.logPlanSteps = logPlanSteps;
		}

		private int getCurrentStep() {
			return currentStep;
		}

		private void setCurrentStep(int currentStep) {
			this.currentStep = currentStep;
		}

		private int getListStamp() {
			return listIndex;
		}

		private void setListStamp(int listStamp) {
			this.listIndex = listStamp;
		}

	}
}
